package com.demoweb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.demoweb.model.dao.BoardDao;
import com.demoweb.model.dao.MemberDao;
import com.demoweb.model.dto.BoardComment;
import com.demoweb.model.dto.Membership;

@WebServlet("/board/writecomment.action")
public class BoardCommentWriteServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//1. dto객체 생성 -> 입력 정보 읽어서 dto객체에 저장
		int boardNo = Integer.parseInt(req.getParameter("boardno"));

		BoardComment comment = new BoardComment();
		comment.setContent(req.getParameter("content"));
		comment.setBoardNo(boardNo);
		comment.setWriter(
			((Membership)req.getSession().getAttribute("loginuser")).getMemberId());

		//2. 데이터베이스에 데이터 insert
		BoardDao dao = new BoardDao();
		dao.insertComment(comment);

		//3. boardview로 이동
		String pageNo = req.getParameter("pageno");
		resp.sendRedirect(
			"view.action?boardno=" + boardNo + "&pageno=" + pageNo);
		
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		doGet(req, resp);
	}
	
}




